<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能公厕 - 详情</title>
    <style>
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 15px;
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .back-btn {
            font-size: 20px;
            cursor: pointer;
            width: 40px;
            display: flex;
            align-items: center;
        }
        .header h2 {
            margin: 0;
            flex: 1;
            text-align: center;
        }
        .stats-btn {
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: #666;
            background: none;
            border: none;
            padding: 0;
        }
        .stats-btn i {
            font-size: 20px;
        }
        .stats-btn::before,
        .stats-btn::after {
            display: none;
        }
        .grid-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
            padding: 15px;
        }
        .grid-item {
            position: relative;
            width: 100%;
            padding-bottom: 100%;
            background: white;
            border: 1px solid #eee;
            border-radius: 8px;
            cursor: pointer;
            overflow: hidden;
            transition: all 0.3s ease;
        }
        
        /* 空置状态样式 */
        .grid-item.empty {
            background-color: #e8f5e9;  /* 浅绿色背景 */
            border: 1px solid #4CAF50;  /* 绿色边框 */
            color: #4CAF50;  /* 绿色文字 */
        }
        
        /* 占用状态样式 */
        .grid-item.occupied {
            background-color: white;
            border: 1px solid #ddd;
            color: #333;
            cursor: default;  /* 保持默认鼠标样式 */
        }
        
        /* 性别角标样式 */
        .gender-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            padding: 4px 8px;
            border-radius: 10px;
            font-size: 12px;
            color: white;
            z-index: 2;
        }
        
        .gender-badge.male {
            background: #1976d2;
        }
        
        .gender-badge.female {
            background: #d81b60;
        }
        
        /* 添加开锁按钮样式 */
        .unlock-btn {
            position: absolute;
            right: 8px;
            bottom: 8px;
            background: none;
            border: none;
            color: #4CAF50;
            cursor: pointer;
            padding: 0;
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: color 0.3s;
        }

        .unlock-btn:hover {
            color: #45a049;
        }

        /* 状态图标样式保持不变 */
        .status-icon {
            position: absolute;
            right: 8px;
            bottom: 8px;
            font-size: 20px;
        }

        /* 调整空置状态的文字位置 */
        .grid-item.empty .status-text {
            margin-bottom: 25px;  /* 为按钮留出空间 */
        }

        /* 添加加载动画样式 */
        .loading {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
            color: white;
        }

        /* 添加编号角标样式 */
        .number-badge {
            position: absolute;
            top: 2px;
            left: 2px;
            font-size: 12px;
            padding: 2px 4px;
            border-radius: 4px;
            background: rgba(0, 0, 0, 0.1);
            color: #666;
        }

        /* 调整性别角标位置，确保不重叠 */
        .gender-badge {
            position: absolute;
            top: 2px;
            right: 2px;
            font-size: 12px;
            padding: 2px 4px;
            border-radius: 4px;
            background: rgba(255, 255, 255, 0.9);
        }

        /* 调整状态文字位置 */
        .status-text {
            margin-top: 8px;  /* 为顶部角标留出空间 */
        }

        /* 确保空置状态下按钮位置正确 */
        .grid-item.empty .status-text {
            margin-bottom: 25px;
        }

        /* 状态样式 */
        .grid-item.empty {
            background-color: #e8f5e9;
            border: 1px solid #4CAF50;
            color: #4CAF50;
        }

        .grid-item.occupied {
            background-color: white;
            border: 1px solid #ddd;
            color: #333;
        }

        .grid-item.fault {
            background-color: #fff3e0;
            border: 1px solid #ff9800;
            color: #ff9800;
        }

        .grid-item.maintenance {
            background-color: #e3f2fd;
            border: 1px solid #2196f3;
            color: #2196f3;
        }

        .grid-item.disabled {
            background-color: #f5f5f5;
            border: 1px solid #9e9e9e;
            color: #9e9e9e;
        }

        .grid-item.smoke {
            background-color: #ffebee;
            border: 1px solid #f44336;
            color: #f44336;
        }

        /* 状态图标样式 */
        .status-icon {
            font-size: 14px;
            margin-right: 4px;
        }

        /* 警告图标样式 */
        .warning-icon {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 24px;
            opacity: 0.2;
        }

        /* 操作菜单样式 */
        .action-menu {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: white;
            border-radius: 16px 16px 0 0;
            padding: 16px;
            box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .action-menu button {
            padding: 12px;
            border: none;
            background: #f5f5f5;
            border-radius: 8px;
            font-size: 16px;
            color: #333;
            cursor: pointer;
        }

        .action-menu button:last-child {
            margin-top: 8px;
            background: #ff4d4f;
            color: white;
        }

        .action-menu button:active {
            opacity: 0.8;
        }

        /* 可交互厕位的样式 */
        .grid-item.interactive {
            cursor: pointer;
        }
        
        .grid-item.interactive:hover {
            opacity: 0.9;
        }
        
        /* 占用状态厕位的样式 */
        .grid-item.occupied {
            background-color: white;
            border: 1px solid #ddd;
            color: #333;
            cursor: default;  /* 保持默认鼠标样式 */
        }

        /* 确保占用状态下不显示任何hover效果 */
        .grid-item.occupied:hover {
            opacity: 1;
        }

        /* 按钮样式 */
        .stats-btn {
            padding: 8px;
            cursor: pointer;
            color: #007bff;
        }

        /* 统计弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        .modal-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 8px;
            width: 90%;
            max-width: 400px;
            max-height: 80vh;
            overflow-y: auto;
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .stats-filters {
            margin-bottom: 15px;
        }

        .stats-filters select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .stats-list {
            display: grid;
            gap: 10px;
        }

        .stats-item {
            display: flex;
            justify-content: space-between;
            padding: 10px;
            background: #f5f5f5;
            border-radius: 4px;
        }

        .stats-item-info {
            display: flex;
            gap: 10px;
            align-items: center;
        }

        .stats-count {
            color: #007bff;
            font-weight: bold;
        }

        /* 统计总计样式 */
        .stats-summary {
            margin-bottom: 20px;
        }

        .stats-summary h4 {
            margin: 0 0 10px 0;
            color: #333;
        }

        .stats-total {
            display: flex;
            gap: 20px;
            margin-top: 10px;
        }

        .stats-total-item {
            flex: 1;
            padding: 15px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
            font-weight: bold;
        }

        .stats-total-item.male {
            background-color: #e3f2fd;
            color: #1976d2;
        }

        .stats-total-item.female {
            background-color: #fce4ec;
            color: #c2185b;
        }

        .stats-total-item i {
            font-size: 20px;
        }

        /* 分割线 */
        .stats-divider {
            height: 1px;
            background: #eee;
            margin: 20px 0;
        }

        /* 详细统计标题 */
        .stats-details h4 {
            margin: 0 0 15px 0;
            color: #333;
        }

        /* 优化现有样式 */
        .stats-item {
            background: #f8f9fa;
            padding: 12px 15px;
            border-radius: 6px;
            border: 1px solid #eee;
        }

        .stats-item:hover {
            background: #f0f0f0;
        }

        .no-data {
            text-align: center;
            color: #666;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 6px;
        }

        /* 修改关闭按钮样式，确保不继承其他样式 */
        .modal .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
            background: none;
            border: none;
            padding: 0;
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            /* 移除可能的继承样式 */
            position: static;
            top: auto;
            right: auto;
            border-radius: 0;
        }

        /* 确保关闭按钮不会显示任何额外的装饰 */
        .modal .close-btn::before,
        .modal .close-btn::after {
            display: none;
        }

        /* 移除任何可能的背景色和边框 */
        .modal .close-btn {
            background: transparent !important;
            border: none !important;
        }

        .stall-number {
            position: absolute;
            top: -4px;
            left: -4px;
            background: #666;
            color: white;
            font-size: 16px;
            width: 28px;
            height: 28px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
            z-index: 2;
        }

        .stall-info {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 60px;
            padding-top: 10px;
        }

        .status-text {
            display: flex;
            align-items: center;
            gap: 5px;
            font-size: 14px;
            margin-top: 5px;
            white-space: nowrap;  /* 防止文字换行 */
        }

        /* 状态颜色 */
        .empty .stall-number {
            background: #4caf50;
        }

        .occupied .stall-number {
            background: #f44336;
        }

        .fault .stall-number {
            background: #ff9800;
        }

        .maintenance .stall-number {
            background: #2196f3;
        }

        .smoke .stall-number {
            background: #9c27b0;
        }

        /* 开锁按钮样式调整 */
        .unlock-btn {
            margin-top: 8px;
            padding: 4px 12px;
            border: none;
            border-radius: 4px;
            background: #4caf50;
            color: white;
            font-size: 12px;
            cursor: pointer;
            white-space: nowrap;  /* 防止文字换行 */
            line-height: normal;  /* 确保文字垂直居中 */
            height: 24px;         /* 固定高度 */
            display: flex;        /* 使用flex布局 */
            align-items: center;  /* 垂直居中 */
            justify-content: center; /* 水平居中 */
        }

        .unlock-btn:hover {
            background: #45a049;
        }

        /* 修改评分按钮样式 */
        .rate-btn {
            position: fixed;
            right: 20px;
            bottom: 80px;  /* 避免被底部导航栏遮挡 */
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #ffa000;
            color: white;
            border: none;
            box-shadow: 0 2px 8px rgba(255, 160, 0, 0.3);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            z-index: 99;
            padding: 0;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .rate-btn i {
            font-size: 18px;
            margin-bottom: 2px;
        }

        .rate-btn span {
            font-size: 12px;
            line-height: 1;
        }

        /* 按钮悬停效果 */
        .rate-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(255, 160, 0, 0.4);
        }

        /* 按钮点击效果 */
        .rate-btn:active {
            transform: translateY(0);
            box-shadow: 0 2px 8px rgba(255, 160, 0, 0.3);
        }

        /* 评分对话框样式 */
        .rating-dialog {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            z-index: 1000;
            width: 80%;
            max-width: 300px;
        }

        .rating-dialog h3 {
            margin: 0 0 15px 0;
            text-align: center;
            color: #333;
        }

        .rating-stars {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin: 20px 0;
        }

        .rating-stars i {
            font-size: 24px;
            color: #ddd;
            cursor: pointer;
        }

        .rating-stars i.active {
            color: #ffa000;
        }

        .rating-dialog-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }

        .rating-dialog-buttons button {
            padding: 6px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .rating-dialog-buttons .submit {
            background: #4caf50;
            color: white;
        }

        .rating-dialog-buttons .cancel {
            background: #f5f5f5;
            color: #666;
        }

        /* 遮罩层 */
        .overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
        }

        /* Toast 样式 */
        .toast {
            position: fixed;
            left: 50%;
            bottom: 100px;  /* 位于评分按钮上方 */
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 10px 20px;
            border-radius: 20px;
            font-size: 14px;
            z-index: 1000;
            opacity: 0;
            transition: opacity 0.3s;
            pointer-events: none;  /* 防止toast影响其他元素的点击 */
        }

        .toast.show {
            opacity: 1;
        }

        /* 成功和错误状态 */
        .toast.success {
            background: rgba(76, 175, 80, 0.9);
        }

        .toast.error {
            background: rgba(244, 67, 54, 0.9);
        }

        /* 管理员菜单样式 */
        .admin-menu {
            position: fixed;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 8px 0;
            z-index: 1000;
            min-width: 120px;
        }

        .menu-item {
            padding: 8px 16px;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            transition: background-color 0.2s;
        }

        .menu-item:hover {
            background: #f5f5f5;
        }

        .menu-item.active {
            color: #4caf50;
        }

        .menu-item i {
            width: 16px;
            text-align: center;
        }

        /* 修改悬浮按钮容器样式 */
        .float-btns {
            position: fixed;
            right: 20px;
            bottom: 20px;
            display: flex;
            flex-direction: column;
            gap: 10px;
            z-index: 1000;
        }

        /* 统一悬浮按钮样式 */
        .float-btn {
            width: 56px;
            height: 56px;
            border-radius: 28px;
            color: white;
            border: none;
            box-shadow: 0 2px 8px rgba(0,0,0,0.3);
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            transition: all 0.3s;
        }

        .float-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.4);
        }

        .float-btn:active {
            transform: translateY(0);
        }

        /* 评分按钮样式 */
        .float-btn.rating {
            background: #FFC107;
        }

        /* 新增按钮样式 */
        .float-btn.add {
            background: #2196F3;
        }

        /* 新增按钮样式 */
        .add-btn {
            width: 40px;
            height: 40px;
            border-radius: 20px;
            background: #2196F3;
            color: white;
            border: none;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 18px;
            transition: all 0.3s;
        }

        .add-btn:hover {
            background: #1976D2;
            transform: scale(1.05);
        }

        .add-btn:active {
            background: #1565C0;
            transform: scale(0.95);
        }

        /* 修改厕位样式 */
        .grid-item {
            position: relative;
            width: 100%;
            padding-bottom: 100%;
            background: white;
            border: 1px solid #eee;
            border-radius: 8px;
            cursor: pointer;
            overflow: hidden;
            transition: all 0.3s ease;
        }

        /* 性别图标样式 */
        .stall-gender {
            position: absolute;
            top: 8px; /* 右上角 */
            left: 8px;
            font-size: 20px;  /* 调整图标大小 */
        }

        .stall-gender i.fa-male {
            color: #1976d2;
        }

        .stall-gender i.fa-female {
            color: #d81b60;
        }

        /* 状态文字和编号样式 */
        .status-number {
            position: absolute;
            top: 50%; /* 中间位置 */
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 16px; /* 放大编号 */
            color: #666; /* 颜色调整 */
            text-align: center;
        }

        /* 状态图标样式 */
        .status-icon {
            position: absolute;
            right: 8px;
            bottom: 8px;
            font-size: 20px;
        }

        /* 不同状态的样式 */
        .grid-item.empty {
            background: #e8f5e9; /* 偏绿的背景色 */
        }

        .grid-item.occupied {
            background: #f5f5f5;
        }

        .grid-item.fault {
            background: #ffebee;
        }

        .grid-item.maintenance {
            background: #fff3e0;
        }

        .grid-item.smoke {
            background: #efebe9;
        }

        .grid-item.disabled {
            opacity: 0.7;
            cursor: not-allowed;
            background: #f5f5f5;
        }

        /* 添加内容容器以确保内容正确定位 */
        .grid-item-content {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 10px;
        }
    </style>
    <!-- 添加Font Awesome图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="js/toast.js"></script>
</head>
<body>
    <div class="header">
        <i class="fas fa-arrow-left back-btn" onclick="goBack()"></i>
        <h2>厕位列表</h2>
        <button id="statsBtn" class="stats-btn" onclick="showStatsModal()">
            <i class="fas fa-chart-bar"></i>
        </button>
        <button id="addStallBtn" class="add-btn" style="display: none;">
            <i class="fas fa-plus"></i>
        </button>
    </div>
    <div class="grid-container" id="statusGrid">
        ${currentToilet.stalls.map((stall, index) => {
            return `
                <div class="grid-item ${stall.state}" onclick="goToStallDetail(${index + 1}, '${stall.status}', '${stall.gender}')">
                    <div class="stall-number">${index + 1}</div>
                    <div class="grid-item-content">
                        <div class="gender-badge ${stall.gender === '男' ? 'male' : 'female'}">
                            ${stall.gender}
                        </div>
                        <div class="stall-info">
                            <span class="status-text">
                                ${stall.status}
                            </span>
                            ${stall.state === 'empty' ? `
                                <button class="unlock-btn" onclick="handleUnlock(event, ${index + 1}, '${stall.gender}')">
                                    <i class="fas fa-unlock"></i>
                                </button>
                            ` : `
                                <div class="status-icon">
                                    ${getStatusIcon(stall.state)}
                                </div>
                            `}
                        </div>
                    </div>
                </div>
            `;
        }).join('')}
    </div>
    <script type="module" src="js/detail.js"></script>
    <!-- 添加加载提示 -->
    <div id="loading" class="loading">
        <div>开锁中...</div>
    </div>
    <!-- 添加统计弹窗 -->
    <div id="statsModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>开锁统计</h3>
                <span class="close-btn" onclick="closeStatsModal()">&times;</span>
            </div>
            <div class="modal-body">
                <div class="stats-filters">
                    <select id="statsFilter" onchange="updateStatsView()">
                        <option value="today">今日统计</option>
                        <option value="week">本周统计</option>
                        <option value="month">本月统计</option>
                    </select>
                </div>
                <div id="statsList" class="stats-list"></div>
            </div>
        </div>
    </div>
    <!-- 修改评分按钮 HTML -->
    <div class="float-btns">
        <button class="float-btn rating" onclick="showRatingDialog()">
            <i class="fas fa-star"></i>
        </button>
        <button id="addStallBtn" class="float-btn add" style="display: none;">
            <i class="fas fa-plus"></i>
        </button>
    </div>

    <!-- 评分对话框 -->
    <div class="overlay" id="overlay"></div>
    <div id="ratingDialog" class="rating-dialog">
        <h3>为该公厕评分</h3>
        <div class="rating-stars" id="ratingStars">
            <i class="fas fa-star" data-score="2"></i>
            <i class="fas fa-star" data-score="4"></i>
            <i class="fas fa-star" data-score="6"></i>
            <i class="fas fa-star" data-score="8"></i>
            <i class="fas fa-star" data-score="10"></i>
        </div>
        <div class="rating-dialog-buttons">
            <button class="cancel" onclick="hideRatingDialog()">取消</button>
            <button class="submit" onclick="submitRating()">提交</button>
        </div>
    </div>

    <!-- 然后引入 detail.js -->
    <script src="js/detail.js"></script>

    <!-- 最后添加事件监听 -->
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // 获取当前公厕数据
            const currentToilet = JSON.parse(localStorage.getItem('currentToilet'));
            if (!currentToilet) {
                showToast('获取数据失败', 'error');
                setTimeout(() => {
                    window.location.href = 'home.html';
                }, 1500);
                return;
            }

            // 更新页面标题
            document.querySelector('.header h2').textContent = currentToilet.name;

            // 渲染厕位列表
            const gridContainer = document.getElementById('statusGrid');
            if (gridContainer && currentToilet.stalls) {
                gridContainer.innerHTML = currentToilet.stalls.map((stall, index) => {
                    return `
                        <div class="grid-item ${stall.state}" onclick="goToStallDetail(${index + 1}, '${stall.status}', '${stall.gender}')">
                            <div class="stall-number">${index + 1}</div>
                            <div class="grid-item-content">
                                <div class="gender-badge ${stall.gender === '男' ? 'male' : 'female'}">
                                    ${stall.gender}
                                </div>
                                <div class="stall-info">
                                    <span class="status-text">
                                        ${stall.status}
                                    </span>
                                    ${stall.state === 'empty' ? `
                                        <button class="unlock-btn" onclick="handleUnlock(event, ${index + 1}, '${stall.gender}')">
                                            <i class="fas fa-unlock"></i>
                                        </button>
                                    ` : `
                                        <div class="status-icon">
                                            ${getStatusIcon(stall.state)}
                                        </div>
                                    `}
                                </div>
                            </div>
                        </div>
                    `;
                }).join('');
            } else {
                showToast('初始化页面失败', 'error');
                setTimeout(() => {
                    window.location.href = 'home.html';
                }, 1500);
                return;
            }

            // 显示/隐藏添加厕位按钮
            const addStallBtn = document.getElementById('addStallBtn');
            if (localStorage.getItem('isAdmin') === 'true') {
                addStallBtn.style.display = 'flex';
                
                // 添加点击事件监听器
                addStallBtn.addEventListener('click', () => {
                    if (currentToilet) {
                        const returnUrl = `detail.html?name=${encodeURIComponent(currentToilet.name)}`;
                        window.location.href = `add-stall.html?toilet=${encodeURIComponent(currentToilet.name)}&return=${encodeURIComponent(returnUrl)}`;
                    }
                });
            }
        });

        // 跳转到厕位详情页
        function goToStallDetail(stallId, status, gender) {
            const currentToilet = JSON.parse(localStorage.getItem('currentToilet'));
            if (!currentToilet) return;

            const params = new URLSearchParams();
            params.set('stallId', stallId);
            params.set('toilet', currentToilet.name);
            params.set('id', stallId);
            window.location.href = `stall-detail.html?${params.toString()}`;
        }

        // 返回首页
        function goBack() {
            window.location.href = 'home.html';
        }

        // 修改 getStatusIcon 函数
        function getStatusIcon(state) {
            const icons = {
                empty: '<i class="fas fa-door-open" style="color: #4CAF50;"></i>',
                occupied: '<i class="fas fa-user-lock" style="color: #f44336;"></i>', // 更改为用户锁定图标
                fault: '<i class="fas fa-exclamation-triangle" style="color: #ff9800;"></i>',
                maintenance: '<i class="fas fa-tools" style="color: #2196f3;"></i>',
                disabled: '<i class="fas fa-ban" style="color: #9e9e9e;"></i>',
                smoke: '<i class="fas fa-smoking" style="color: #f44336;"></i>'
            };
            return icons[state] || '';
        }

        function handleUnlock(event, stallId, gender) {
            event.stopPropagation();
            
            if (confirm(`确认要开启${stallId}号${gender}厕所吗？`)) {
                const loading = document.getElementById('loading');
                loading.style.display = 'flex';
                
                setTimeout(() => {
                    loading.style.display = 'none';
                    
                    // 获取当前公厕数据并更新状态
                    const currentToilet = JSON.parse(localStorage.getItem('currentToilet'));
                    if (currentToilet && currentToilet.stalls[stallId - 1]) {
                        // 更新厕位状态
                        currentToilet.stalls[stallId - 1].status = '占用';
                        currentToilet.stalls[stallId - 1].state = 'occupied';
                        currentToilet.stalls[stallId - 1].unlockTime = Date.now();
                        
                        // 保存更新后的数据
                        localStorage.setItem('currentToilet', JSON.stringify(currentToilet));
                        
                        // 同步更新 toiletsData
                        const toiletsData = JSON.parse(localStorage.getItem('toiletsData')) || [];
                        const toiletIndex = toiletsData.findIndex(t => t.name === currentToilet.name);
                        if (toiletIndex !== -1) {
                            toiletsData[toiletIndex] = currentToilet;
                            localStorage.setItem('toiletsData', JSON.stringify(toiletsData));
                            
                            // 触发状态更新事件
                            window.dispatchEvent(new CustomEvent('stallStatusUpdate', {
                                detail: {
                                    toiletName: currentToilet.name,
                                    stallId: stallId,
                                    newState: 'occupied',
                                    newStatus: '占用'
                                }
                            }));
                        }

                        // 更新UI和显示提示...
                    }
                }, 1500);
            }
        }

        // 添加状态更新监听器
        window.addEventListener('stallStatusUpdate', (event) => {
            const { toiletName, stallId, newState, newStatus } = event.detail;
            updateStallStatus(toiletName, stallId, newState, newStatus);
        });

        // 状态更新函数
        function updateStallStatus(toiletName, stallId, newState, newStatus) {
            // 更新 toiletsData
            const toiletsData = JSON.parse(localStorage.getItem('toiletsData')) || [];
            const toiletIndex = toiletsData.findIndex(t => t.name === toiletName);
            
            if (toiletIndex !== -1) {
                toiletsData[toiletIndex].stalls[stallId - 1].state = newState;
                toiletsData[toiletIndex].stalls[stallId - 1].status = newStatus;
                localStorage.setItem('toiletsData', JSON.stringify(toiletsData));
                
                // 如果是当前显示的公厕，更新 currentToilet
                const currentToilet = JSON.parse(localStorage.getItem('currentToilet'));
                if (currentToilet && currentToilet.name === toiletName) {
                    currentToilet.stalls[stallId - 1].state = newState;
                    currentToilet.stalls[stallId - 1].status = newStatus;
                    localStorage.setItem('currentToilet', JSON.stringify(currentToilet));
                }
            }
        }
    </script>

    <!-- 添加 Toast 容器 -->
    <div id="toast" class="toast"></div>
</body>
</html> 